package com.example.ordersservice.Api;


import com.example.commonutils.R;
import com.example.ordersservice.entity.OrderOrders;
import com.example.ordersservice.service.OrderOrdersService;
import com.example.ordersservice.service.OrderPayLogService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-07-06
 */
@RestController
@RequestMapping("/ordersservice/orderPayLog")
public class OrderPayLogController {
    @Autowired
    private OrderPayLogService payLogService;
    @Autowired
    private OrderOrdersService ordersService;
    //生成微信支付二维码接口
    @GetMapping("createNative/{ordersNumber}")
    public R createNative(@PathVariable String ordersNumber){
        //返回信息，包含二维码地址
        Map map = payLogService.createNative(ordersNumber);
        return R.ok().data(map);
    }
    //查询订单支付状态
    @GetMapping("queryPayStatus/{ordersNumber}")
    public R queryPayStatus(@PathVariable String ordersNumber){
        Map<String,String> map = payLogService.queryPayStatus(ordersNumber);
        if (map == null){
            return R.error().message("支付出错了");
        }
        if (map.get("trade_state").equals("SUCCESS")){
            //支付成功后，更新订单状态，记录支付日志
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }
        return R.ok().code(25000).message("支付中");
    }
}

